package com.example.Sao.Tools;

import android.util.Log;

import com.example.Sao.MyActivity.MyApplication;
import com.example.Sao.ObjectBox.DeviceEntity;
import com.example.Sao.ObjectBox.DeviceEntity_;
import com.google.gson.Gson;

import java.util.List;

import javax.annotation.Nullable;

import io.objectbox.Box;
import io.objectbox.BoxStore;
import io.objectbox.Property;
import io.objectbox.TxCallback;
import io.objectbox.query.Query;
import io.objectbox.query.QueryBuilder;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: scan
 * @Package: com.example.MyApplication.SqlLite
 * @ClassName: ObjectBoxUtil
 * @Description: java类作用描述
 * @Author: 作者名
 * @CreateDate: 2020/4/21 23:19
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/4/21 23:19
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class ObjectBoxHelper {
    private static ObjectBoxHelper objectBoxHelper = null;
    private  boolean isSucces;
    private final String TAG="ObjectBoxUtil";
    private Box<DeviceEntity>beanbox;
    private BoxStore boxStore;
    private MyApplication application;

    public ObjectBoxHelper(){
        application = MyApplication.getMyApplication();
        boxStore =application.getBoxStore();
        beanbox= boxStore.boxFor(DeviceEntity.class);
    }

    public static ObjectBoxHelper getInstance(){
        if(objectBoxHelper==null){
            objectBoxHelper = new ObjectBoxHelper();
        }
        return objectBoxHelper;
    }
    /**插入一条数据
     * @param device
     */
    public  void add(DeviceEntity device){
        if (beanbox!=null)
        beanbox.put(device);
    }
     public boolean RxAdd(DeviceEntity device){
         boxStore.runInTxAsync(new Runnable() {
             @Override
             public void run() {
                 //子线程进入Json转bean和入库操作
                 add(device);
             }
         }, new TxCallback<Void>() {
             @Override
             public void txFinished(@Nullable Void result, @Nullable Throwable error) {
                 if (error==null){
                     isSucces = true;
                     Log.d(TAG, "txFinished: " + "添加成功");
                 }else {
                     isSucces = false;
                     Log.d(TAG, "txFinished: " + "添加失败");
                 }
             }
         });
         return isSucces;
     }
    /**插入多条
     * @param deviceEntityList
     */
    public  void addlist(List<DeviceEntity>deviceEntityList){
        if (beanbox!=null) {
            beanbox.put(deviceEntityList);
        }else {
            Log.d(TAG,"beanbox is null");
        }
    }

    /**删除指定id
     * @param id
     */
    public void remove(long id){
        if (beanbox!=null)
        beanbox.remove(id);
    }

    /**
     * 删除全部数据
     */
    public  void removeall(){
        if (beanbox!=null)
            beanbox.removeAll();
    }

    /**根据条件名称查询
     * @param condition
     * @return
     */
    public  List<DeviceEntity> Query(String condition,Property<DeviceEntity>property){
        if (beanbox!=null) {
            Query<DeviceEntity> query = beanbox.query().equal(property, "").build();
            return query.setParameter(property, condition).find();
        }else {
            return null;
        }
    }

    /**根据状态名称查询
     * @param staute
     * @return
     */
    public  List<DeviceEntity> Query(Boolean staute,Property<DeviceEntity>property){
        if (beanbox!=null) {
            Query<DeviceEntity> query = beanbox.query().equal(property, "").build();
            return query.setParameter(property, staute).find();
        }else {
            return null;
        }
    }

    /**根据设备名称以及部门查询
     * @param device
     * @return
     */
    public  List<DeviceEntity> Query(String device,String partment,Property<DeviceEntity>property,Property<DeviceEntity>property2){
        if (beanbox!=null) {
            QueryBuilder<DeviceEntity>builder = beanbox.query();
            builder.equal(property2,partment)
                    .startsWith(property,device);
            return builder.build().find();
        }else {
            return null;
        }
    }
    /**Rx监听插入操作
     * @param deviceEntityList
     * @return
     */
    public  boolean RxAddList(List<DeviceEntity>deviceEntityList){
        boxStore.runInTxAsync(new Runnable() {
            @Override
            public void run() {
                //子线程进入Json转bean和入库操作
                addlist(deviceEntityList);
            }
        }, new TxCallback<Void>() {
            @Override
            public void txFinished(@Nullable Void result, @Nullable Throwable error) {
                if (error==null){
                    isSucces = true;
                    Log.d(TAG, "txFinished: " + "添加成功");
                }else {
                    isSucces = false;
                    Log.d(TAG, "txFinished: " + "添加失败");
                }
            }
        });
        return isSucces;
    }

    /**Rx监听查询操作
     * @param condition
     * @return
     */
    public Observable RxQuery(String condition,Property<DeviceEntity>property){
         return Observable.create(new ObservableOnSubscribe<List<DeviceEntity>>() {
              @Override
              public void subscribe(ObservableEmitter<List<DeviceEntity>> e) throws Exception {
                  e.onNext(Query(condition,property));
              }
          }).subscribeOn(Schedulers.newThread())
                  .observeOn(AndroidSchedulers.mainThread());
    }
    /**Rx监听查询操作
     * @param condition
     * @return
     */
    public Observable RxQuery(String condition,String partment,Property<DeviceEntity>property,Property<DeviceEntity>property2){
        return Observable.create(new ObservableOnSubscribe<List<DeviceEntity>>() {
            @Override
            public void subscribe(ObservableEmitter<List<DeviceEntity>> e) throws Exception {
                List<DeviceEntity>entities =  Query(condition,partment,property,property2);
                Log.d(TAG,"查询结果："+new Gson().toJson(entities)+"查询参数："+condition+partment);
                e.onNext(entities);
            }
        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread());
    }
    /**Rx监听删除操作
     * @return
     */
    public  boolean RxRemove(){
        boxStore.runInTxAsync(new Runnable() {
            @Override
            public void run() {
                //子线程进入Json转bean和入库操作
                removeall();
            }
        }, new TxCallback<Void>() {
            @Override
            public void txFinished(@Nullable Void result, @Nullable Throwable error) {
                if (error==null){
                    isSucces = true;
                    Log.d(TAG, "txFinished: " + "添加成功");
                }else {
                    isSucces = false;
                    Log.d(TAG, "txFinished: " + "添加失败");
                }
            }
        });
        return isSucces;
    }
}
